<?php
class Merchant_ProductController extends Zend_Controller_Action
{
	
	public function init() {//图片上传 取消身份认证
		$action = strtolower($this->_request->getActionName());
		if ($action != 'upload-picture') {
			parent :: init();
		} 
	}
	
	public function preDispatch(){
		$this->tplDirectory = "merchant/product/";
	}
	
	public function indexAction(){
		
	}
	
	public function createAction(){

		$serviceObj = new Merchant_Service_Product();
		$loginInfo = $serviceObj->getLoginInfo();
		$this->view->customerId = $loginInfo->customer["customer_id"];
		
		$selectOptions = $serviceObj->getSelectOptions();
		$this->view->selectOptions = $selectOptions;
		
		if(!$this->_request->isPost()){
			echo Cff::renderTpl($this->tplDirectory."create.tpl","user-layout");
			exit;
		}
		
		$params = $this->_request->getParams();
		//print_r($params);exit;
		$productOptionValues = array();
		foreach($params as $key => $value){
			if(strpos($key,"autoGenerate_") !== false){
				$keyArr = explode("_",$key);//keyArr[1] 为po_id
				$productOptionValue = array(
					"po_id" => $keyArr[1],
					"poc_id" => $value
				);
				$productOptionValues[$keyArr[1]] = $productOptionValue;
			}
		}
		$product_is_qc = $this->_request->getParam('product_is_qc',0);
		$po_id = $this->_request->getParam('poid','');
		$poc_ids = $this->_request->getParam('pocid',array());
		$op_values = $this->_request->getparam('opvalue',array());
		
// 		//qcArr start
 			$qcArr = array();
	 		$qcCon = array('po_id'=>$po_id,'poc_ids'=>$poc_ids,'op_values'=>$op_values);
	 		if(!empty($qcCon['poc_ids']) && !empty($qcCon['op_values'])){
	 			foreach ($qcCon['poc_ids'] as $key=>$poc_id){
	 				foreach($qcCon['op_values'][$key] as $v){
	 					$qcArr[] = (array('po_id'=>$qcCon['po_id'],'poc_id'=>$poc_id,'po_value'=>$v));
	 				}
	 			}
	 		}	 

		$row = array(
			// $productCategory = trim($this->_request->getParam("productCategory","");//产品种类
			"product_is_qc" =>0,
			"product_title" => trim($this->_request->getParam("productName","")),//产品名称
			"product_title_cn" => trim($this->_request->getParam("productCname","")),//产品中文名称
			"product_sku" => trim($this->_request->getParam("productNumber","")),//产品编号
			"product_pack_unit" => trim($this->_request->getParam("productFlat","")),//产品单位
			"product_weight" => trim($this->_request->getParam("weight","")),//产品重量
			"product_length" => trim($this->_request->getParam("lenght","")),//产品长
			"product_width" => trim($this->_request->getParam("width","")),//宽
			"product_height" => trim($this->_request->getParam("height","")),//高
			"product_declared_value" => trim($this->_request->getParam("declareWorth","")),//申报价值
			// $declareName = trim($this->_request->getParam("declareName",""));//申报品名
		);
		$pictures = $this->_request->getParam("picture",array());
		$links = $this->_request->getParam("link",array());
		$pSku = trim($this->_request->getParam("productNumber",""));
		
		
        $create = $serviceObj->create($row,$productOptionValues,$pictures,$links,null,null,$qcArr);
		if($create === true){
			$return['ask'] = 1;
			$return['message'] = "Product_SKU : {$pSku} Add successful.";
			die(Zend_Json::encode($return));
			  
		}elseif(is_array($create)){
			$return['ask'] = 0;
			$return['message'] = $create;
			die(Zend_Json::encode($return));
		}
		
	}
	
	public function editAction(){
		$serviceObj = new Merchant_Service_Product();
		$params = $this->_request->getParams();
		$productOptionValues = array();
		foreach($params as $key => $value){
			if(strpos($key,"autoGenerate_") !== false){
				$keyArr = explode("_",$key);//keyArr[1] 为po_id
				$productOptionValue = array(
					"po_id" => $keyArr[1],
					"poc_id" => $value
				);
				$productOptionValues[$keyArr[1]] = $productOptionValue;
			}
		}
//		print_r($productOptionValues);die();
		$row = array(
			// $productCategory = trim($this->_request->getParam("productCategory","");//产品种类
			"product_title" => trim($this->_request->getParam("productName","")),//产品名称
			"product_title_cn" => trim($this->_request->getParam("productCname","")),//产品中文名称
			"product_sku" => trim($this->_request->getParam("productNumber","")),//产品编号
			"product_pack_unit" => trim($this->_request->getParam("productFlat","")),//产品单位
			"product_weight" => trim($this->_request->getParam("weight","")),//产品重量
			"product_length" => trim($this->_request->getParam("lenght","0.00")),//产品长
			"product_width" => trim($this->_request->getParam("width","0.00")),//宽
			"product_height" => trim($this->_request->getParam("height","0.00")),//高
			"product_declared_value" => trim($this->_request->getParam("declareWorth","")),//申报价值
			"product_is_qc" => trim($this->_request->getParam("product_isqc",0)),//是否质检
			// $declareName = trim($this->_request->getParam("declareName",""));//申报品名
		);
		$pictures = $this->_request->getParam("picture",array());
		$links = $this->_request->getParam("link",array());
		$edit = $serviceObj->update($params['productId'],$row,$productOptionValues,$pictures,$links);
		if($edit === true){//更新成功
			// $this->view->message = I18n::getInstance()->translate("0000015");
			$return = array(
				"ask" => true,
				"result" => "Update successful.",
			);
		}else if($edit === false){//更新失败
				$return = array(
					"ask" => false,
					"result" => array(
						"productOptionValues" => $productOptionValues,
						"row" => $row,
						"pictures" => $pictures,
						"message" => "Update failure.",
					),
				);
		}else if(is_array($edit)){
			$return = array(
					"ask" => false,
					"result" => array(
						"productOptionValues" => $productOptionValues,
						"row" => $row,
						"pictures" => $pictures,
						"errors" => $edit,
					),
				);
		}
		die(json_encode($return));
	}
	
	public function updateQtyAction(){
		$serviceObj = new Merchant_Service_Product();
		$params = $this->_request->getParams();
		$productOptionValues = array();
		foreach($params as $key => $value){
			if(strpos($key,"autoGenerate_") !== false){
				$keyArr = explode("_",$key);//keyArr[1] 为po_id
				$productOptionValue = array(
					"po_id" => $keyArr[1],
					"poc_id" => $value
				);
				$productOptionValues[$keyArr[1]] = $productOptionValue;
			}
		}
		$pictures = $this->_request->getParam("picture",array());
		$links = $this->_request->getParam("link",array());
		$edit = $serviceObj->updateQty($params['productId'],$productOptionValues,$pictures,$links);
		if($edit === true){//更新成功
			// $this->view->message = I18n::getInstance()->translate("0000015");
			$return = array(
				"ask" => true,
				"result" => "Update successful",
			);
		}else if($edit === false){//更新失败
				$return = array(
					"ask" => false,
					"result" => array(
						"productOptionValues" => $productOptionValues,
						"pictures" => $pictures,
						"message" => "Update failure",
					),
				);
		}else if(is_array($edit)){
			$return = array(
					"ask" => false,
					"result" => array(
						"productOptionValues" => $productOptionValues,
						"pictures" => $pictures,
						"errors" => $edit,
					),
				);
		}
		die(json_encode($return));
	}
	
	public function delAction(){
		$productId = $this->_request->productId;
		$serviceObj = new Merchant_Service_Product();
		$result = $serviceObj->delete($productId);
		die(json_encode($result));
	}
	
	public function listAction(){
		$serviceObj = new Merchant_Service_Product();
		$loginInfo = $serviceObj->getLoginInfo();
		$this->view->customerId = $loginInfo->customer["customer_id"];
		$this->view->status = $serviceObj->statusText;
		$total = $serviceObj->getCount();
		$this->view->total = $total;
		echo Cff::renderTpl($this->tplDirectory."list.tpl","user-layout");
	}
	
	public function getListDataAction(){
		$page = $this->_request->getParam("page",1);
		$pageSize = $this->_request->getParam("pageSize",20);
		$condition = array(
				"productSku" => trim($this->_request->getParam("productSku","")),
				"productStatus" => trim($this->_request->getParam("productStatus","")),
				"sortColumn" =>  trim($this->_request->getParam("sortColumn","")),
				"sortMethod" =>  trim($this->_request->getParam("sortMethod","")),
			);
		$serviceObj = new Merchant_Service_Product();
		$rows = $serviceObj->getList($condition,$page,$pageSize);
		$total = $serviceObj->getCount($condition);
		foreach($rows as $key => $row){
			$rows[$key]['product_status'] = $serviceObj->getStatusText($row['product_status']);
			$rows[$key]['category'] = $serviceObj->getProductCategory($row['product_id']);
		}
		$return = array(
			"ask" => true,
			"result" => $rows,
			"total" => $total,
		);
//		print_r($rows);exit;
		die(json_encode($return));
	}
	
	public function viewProductInfoAction(){
		$serviceObj = Merchant_Service_Product::getInstance();
		$productId = $this->_request->productId;
		
		$res = $serviceObj->getProductInfo($productId);
	
		$edit = $this->_request->edit;
		$qcset = $this->_request->qcset;
		$loginInfo = $serviceObj->getLoginInfo();
		$this->view->customerId = $loginInfo->customer["customer_id"];
		$this->view->productId = $productId;
		$this->view->edit = $edit;
		$this->view->qcset = $qcset;
		$this->view->product_is_qc = $res['product_is_qc'];
		
		echo $this->view->render($this->tplDirectory."product-info.tpl");
	}
	
	public function getProductInfoAction(){
		$productId = $this->_request->productId;
		$serviceObj = Merchant_Service_Product::getInstance();
		$product = $serviceObj->getProductInfo($productId);
		$selectOptions = $serviceObj->getSelectOptions();
		if($product === false){//没有找到产品
			$output = array(
				"ask" => false,
				"result" => "No result",
			);
			die(json_encode($output));
		}
		$this->view->productId = $productId;
		$product['product_status'] = $serviceObj->getStatusText($product['product_status']);
		$loginInfo = $serviceObj->getLoginInfo();
		$output = array(
			"ask" => true,
			"result" => array(
				"product" => $product,
				"customerId" => $loginInfo->customer["customer_id"]
			)
		);
		die(json_encode($output));
	}	
	
	public function uploadPictureAction(){
		$tempFile = $_FILES['picture']['tmp_name'];
		//获得图片存储的临时位置
		$serviceObj = new Merchant_Service_Product();
		$str = var_export($tempFile,true);
		self::logError($str);		 
		
		$targetDir = $serviceObj->getPictureUploadDir($this->_request->customerId);
		$targetFile =  $targetDir ."/". $_FILES['picture']['name'];
		
//		$filename = md5($_FILES['picture']['name']."".time())."".preg_replace("/[^\.]*(\.[a-zA-Z]+$)/","\\1",$_FILES['picture']['name']);
//		$targetFile =  $targetDir ."/".$filename;
		$arr = array("ask"=>0);
		if(move_uploaded_file($tempFile,$targetFile)){
			$arr = array("ask"=>1,"filepath"=>1);
			$row = array('product_id'=>0,"pa_path"=>$targetFile,
				"pa_file_type"=>"img","pa_status"=>0,"languages_id"=>1,
				"pa_sort"=>1,"pa_last_update"=>date('Y-m-d H:i:s'));
			$id = Merchant_Service_ProductAttached::add($row);
			
			$arr = array("ask"=>1,
				"picid"=>"p_".$id,
				"filepath"=>"/merchant/product/view-image/fileName/".$_FILES['picture']['name'],
				);
			
		}else{
			$arr = array("ask"=>0,"error"=>'Inner Error');
		}
		echo json_encode($arr);
		exit;
	}
	
	public static function logError($error){
    	$logger = new Zend_Log();
		$uploadDir = APPLICATION_PATH .'/../data/log/test.log';
		$writer = new Zend_Log_Writer_Stream($uploadDir.'err.log');
		$logger->addWriter($writer);
		$logger->info(date('Y-m-d H:i:s').': '.$error." \n");
    }
    
    public function viewImageAction(){
    	$fileName = $this->_request->fileName;
    	$serviceObj = new Merchant_Service_Product();
    
    	$targetDir = $serviceObj->getPictureUploadDir();
    	if($this->_request->productId){//产品编辑，需要加上sku目录
    		$targetDir = $targetDir."/".$this->_request->productId;
    	}
    	header('Content-Type: image/jpeg');
    	echo file_get_contents($targetDir."/".$fileName);
    }
    
    public function adminViewImageAction(){
    	$fileName = $this->_request->fileName;
    	$serviceObj = new Merchant_Service_Product();
    
    	$targetDir = $serviceObj->getPictureUploadDir(null);
    	if($this->_request->customerId){//产品编辑，需要加上
    		$targetDir = $targetDir."/".$this->_request->customerId;
    	}
    	if($this->_request->productId){//产品编辑，需要加上sku目录
    		$targetDir = $targetDir."/".$this->_request->productId;
    	}
    	header('Content-Type: image/jpeg');
    	echo file_get_contents($targetDir."/".$fileName);
    }
    
	public function baseinfoAction(){
		$productId = $this->_request->productId;
		$serviceObj = Merchant_Service_Product::getInstance();
		$product = $serviceObj->getProductInfo($productId);
		$selectOptions = $serviceObj->getSelectOptions();
		if($product !== false){//找到产品
			//查找产品图片
			//判断产品库存
			$ProductStocket = Merchant_Service_ProductStocket::getByProductId($product['product_id']);
			$product['ps_quantity'] = 0;
			if($ProductStocket){
				$product['ps_quantity'] = $ProductStocket['ps_quantity']+$ProductStocket['ps_quantity_hold'];				
			}				
			$pictures = $serviceObj->getProductPicture($productId);
			$links = $serviceObj->getProductLinks($productId);
			$return = array(
				"ask" => true,
				"result" => array(
					"product" => $product,
					"pictures" => $pictures,
					"links" => $links,
					"selectOptions" => $selectOptions,
				),
			);
			die(json_encode($return));
		}
	}
	
	public function getProductPicturesAction(){
		$productId = $this->_request->productId;
		$serviceObj = Merchant_Service_Product::getInstance();
		$pictures = $serviceObj->getAllProductPicture($productId);
		$return = array(
				"ask" => true,
				"result" => array(
					"pictures" => $pictures,
				)
			);
		die(json_encode($return));
	}
	
	public function getProductLogAction(){
		$serviceObj = Merchant_Service_Product::getInstance();
		$page = $this->_request->getParam("page",1);
		$pageSize = $this->_request->getParam("pageSize",20);
		$productId = $this->_request->getParam("productId");
		$logs = $serviceObj->getLog($productId,$page,$pageSize);
		$logCount = $serviceObj->getLogCount($productId);
		$return = array(
			"ask" => true,
			"result" => array(
				"logs" => $logs,
				"total" => $logCount,
			)
		);
		die(json_encode($return));
	}	
	
	public function uploadAction(){
		echo Cff::renderTpl($this->tplDirectory."upload.tpl","user-layout");
	}
	public function picuploadAction(){
		echo Cff::renderTpl($this->tplDirectory."picupload.tpl","user-layout");
	}
	
	public function uploadProcessingAction(){
		$serviceObj = Merchant_Service_Product::getInstance();
		$fileName = $_FILES['productFile']['name'];
		$filePath = $_FILES['productFile']['tmp_name'];
		$fileData = $serviceObj->readUploadFile($fileName,$filePath);
		
		$columnMap = array(
				"SKUNo",
				"SKUName",
				"Category",
				"Length",
				"Height",
				"Width",
				"Weight",
				"Declare Value",
				"Min Qty",
		);
		
		if(is_array($fileData)){
			
			$fileDataTitle = array();
			$pathinfo = pathinfo($fileName);
			if($pathinfo["extension"] == "xls"){
				$fileDataTitle = Merchant_Service_Upload::readEXCEL($filePath);
			}else if($pathinfo["extension"] == "csv"){
				$fileDataTitle = Merchant_Service_Upload::readCSV($filePath);
			}
			$fileDataTitleErr=false;
			
			if($fileDataTitle){
				if(count($fileDataTitle[0])==0){
					$this->view->err = "field name can't is null";
				}else if(count($fileDataTitle[0])!=9){
					$this->view->err = "field is n't  enough";
				}else{
					$tempfield=$fileDataTitle[0];
					foreach ($tempfield as $val){
						if(!in_array($val,$columnMap)){
							$this->view->err = "field name of ['.$val.'] can't is null";
							$fileDataTitleErr=true;
						}
					}
				
				}
			}

			
			if($fileDataTitleErr==false){
				if(!isset($fileData[1])){
					$this->view->err = "file format error or data is null";
				}else{
					if(count($fileData[1])!=9){
						$this->view->err = "field is't not equal to template";
					}else{
						$this->view->products=$fileData;
						$this->view->err = "";
					}
				
				}
			}
			
			
			
		}else{
			$this->view->err = "file format error";
		}
		
		
		$selectOptions = $serviceObj->getSelectOptions();
		$categorys = $selectOptions[0]['options'];
		$this->view->categorys=$categorys;
//		print_r($categorys);
		echo Cff::renderTpl($this->tplDirectory."batchAdd.tpl","user-layout");
//		$this->view->products = $fileData;
//		$this->view->jsonFileDatas = json_encode($fileData);
//		echo $this->view->render($this->tplDirectory."upload-file-edit.tpl");
	}
	
	public function downloadFileAction(){
		$dlfile = $this->_request->dlfile;
		$fullPath = '';
		//方法1
		//绝对路径
		if('xls'==$dlfile) $fullPath = APPLICATION_PATH."/../public".'/product.xls';
		elseif('csv'==$dlfile) $fullPath = APPLICATION_PATH."/../public".'/product.csv' ;
		
		//或者相对路径
		if('xls'==$dlfile) $fullPath = 'product.xls';
		elseif('csv'==$dlfile) $fullPath = 'product.csv' ;
//		echo $fullPath;exit;
		Merchant_Service_Product::getInstance()->downloadFile( $fullPath );
		exit;
		
		//方法2
		$filename = basename($fullPath);
		header("Content-Type: APPLICATION/OCTET-STREAM");
		//Force the download
		$header="Content-Disposition: attachment; filename=".$filename.";";
		header($header );
		// 	header("Content-Transfer-Encoding: binary");
		// 	header("Content-Length: ".$len);
		echo file_get_contents(APPLICATION_PATH."/../public/".$fullPath);
		exit;
	}
	
	/*
	 * 批量添加
	 */
	public function batchAddAction(){
		$params = $this->_request->getParams();
		
//		print_r($params);
		
		$arr = array();
		
		foreach($params as $k=>$v){
			if(is_array($v)){
				foreach($v as $k1=>$v1){
					$arr[$k1][$k] = $v1;
				}
			}
		}
		$a = array(array(),array());
		$rows = array();
		foreach($arr as $k=> $v){
			$category = $v["Category"];
			$category = explode("_",$category);
//			print_r($category);exit;
			$row = array(
				"product_title" => trim($v['SKUName']),//产品名称
				"product_sku" => trim($v['SKUNo']),//产品编号
				"product_weight" => trim($v['product_weight']),//产品重量
				"product_length" => trim($v['product_length']),//产品长
				"product_width" => trim($v['product_width']),//宽
				"product_height" => trim($v['product_height']),//高
				"product_declared_value" => trim($v['product_declared_value']),//申报价值
				"category"=>array(
						"po_id" => $category[0],
						"poc_id" => $category[1]
					),
				"min_qty"=>array(
						"po_id" => 3,
						"poc_id" => $v['Min_Qty']
					),
				
			);	
			$rows[$k] = $row;
		}
		$serviceObj = Merchant_Service_Product::getInstance();
		
//		print_r($rows);
		$result = $serviceObj->batchAdd($rows);	
		if(empty($result)){
			$data = array("ask"=>1,"msg"=>$result);			
		}else{			
			$data = array("ask"=>0,"msg"=>$result);
		}
		
		die(Zend_Json::encode($data));		
	}
	
	public function checkUploadDataAction(){
		$data = $this->_request->getParam("data",array());
		$serviceObj = Merchant_Service_Product::getInstance();
		die(json_encode($serviceObj->checkUploadData($data)));
	}
	
	public function uploadSaveAction(){
		$data = $this->_request->getParam("data",array());
		$serviceObj = Merchant_Service_Product::getInstance();
		die(json_encode($serviceObj->saveUploadData($data)));
	}
	
	public function activateAction(){
		$productIdStr = $this->_request->getParam("productIdStr","");
		$productIds = trim($productIdStr,',');
		$productIds = explode(",",$productIdStr);
		if($productIds[count($productIds)-1] == ""){
			array_pop($productIds);
		}
		$serviceObj = new Merchant_Service_Product();
		$result = $serviceObj->activate($productIds);
		die(json_encode($result));
	}
	
	public function inactivateAction(){
		$productIdStr = $this->_request->getParam("productIdStr","");
		$productIds = explode(",",$productIdStr);
		if($productIds[count($productIds)-1] == ""){
			array_pop($productIds);
		}
		$serviceObj = new Merchant_Service_Product();
		$result = $serviceObj->inactivate($productIds);
		die(json_encode($result));
	}
	
	public function printLabelAction(){
		$params = $this->_request->getParams();
		$rows = array();
		foreach($params as $key => $param){
			if(strpos($key,"count") !== false){
				$rows[substr($key,strpos($key,"count")+5)] = $param;
			}
		}
		$serviceObj = Merchant_Service_Product::getInstance();
		$results = $serviceObj->getPrintInfo($rows);
		$this->view->results = $results;
//		echo $this->view->render($this->tplDirectory."print.tpl");
		
		echo Cff::renderTpl($this->tplDirectory."print1.tpl","layout-blank");
	}

	
	public function printLabel1Action(){//仅做条形码打印测试使用
		$results = array();
		for($i=6;$i<11;$i++){
			$results[] = array(
				"product_sku"=>str_repeat("T",$i),				
				"customer_id"=>"A0317",				
				"poc_value"=>" Toys,Sports & Hobbies",				
			);
		}
		$this->view->results = $results;
//		echo $this->view->render($this->tplDirectory."print.tpl");
		
		echo Cff::renderTpl($this->tplDirectory."print1.tpl","layout-blank");
	}

	
	public function barcodeAction(){
		Merchant_Service_Barcode::barcode($this->_request->code);
	}
	
	public function validateSkuAction(){
		if(trim($this->_request->productNumber) != ""){
			$result = Merchant_Service_Product::getInstance()->existsSku(null,trim($this->_request->productNumber));
			die(json_encode(!$result));
		}
	}
	
	public function getInventoryAction(){
		$productId = $this->_request->productId;
		$wp = Merchant_Service_WarehouseProducts::getByProductId($productId);
		$return = array("ask" => false);
		if($wp){
			$return["ask"] = true;
			$return["result"] = $wp;
		}
		die(json_encode($return));
	}

	public function getOrdersAction(){	//与产品相关的订单	
		$page = $this->_request->getParam("page",1);
		$pageSize = $this->_request->getParam("pageSize",20);
		$productId = $this->_request->getParam("productId");
		
		$condition = array("product_id"=>$productId);
		$logs = Merchant_Service_OrderProduct::getByCondition($condition,'*',"",$page,$pageSize,false);
		$logCount = Merchant_Service_OrderProduct::getByCondition($condition,'count(*)');
		foreach($logs as $k=>$v){
			$order = Merchant_Service_Orders::getByValue($v['orders_code'],"orders_code");
			$logs[$k]['order_status'] = Merchant_Service_Orders::$orderStatus[$order['orders_status']];
			$logs[$k]['add_time'] = $order['add_time'];
		}
		$return = array(
			"ask" => true,
			"result" => array(
				"logs" => $logs,
				"total" => $logCount,
			)
		);
		die(json_encode($return));
	}
	
	public function getOptionChooseAction(){
		$serviceObj = new Merchant_Service_Product();
		$selectOptions = $serviceObj->getSelectOptions();
		$objConfig = new Default_Model_Config();
		$congifRes = $objConfig->getByValue('QUANTITY_CONTROL_ID');
		$return = '';
		foreach($selectOptions as $key=>$val){
			if($val['id']!= $congifRes['config_value']) unset($selectOptions[$key]);
			else $return = $val['options'];
		}
		//print_r($return);exit;
		die(json_encode($return));
	}
	
	public function getQuanlityControlAction(){
		$serviceObj = new Merchant_Service_Product();
		$povServiceObj = new Merchant_Service_ProductOptionValue();
		$p_id = $this->_request->getParam("productId",'');
		$selectOptions = $serviceObj->getSelectOptions();
		$objConfig = new Default_Model_Config();
		$congifRes = $objConfig->getByValue('QUANTITY_CONTROL_ID');
		$return = array();
		foreach($selectOptions as $key=>$val){
			if($val['id']!= $congifRes['config_value']) unset($selectOptions[$key]);
			else{ 
				foreach( $val['options'] as $v ) { 
					$res = $povServiceObj->getByCondition($p_id,$v['po_id'],$v['poc_id']);
					$temp = array();
					foreach($res as $val){
						$temp[] = $val['po_value'];		
					}
					$v['po_value']= $temp;
					$return[]=$v;
				}
			}
		}
		die(json_encode($return));
	}
	
	public function quanlityControlEditAction(){
		 $povServiceObj = new Merchant_Service_ProductOptionValue();
		 $params = $this->_request->getParams();
		 $p_id = $params['productId'];
		 $po_id = $params['poid'];
		 $poc_ids = $this->_request->getParam('pocid','');
		 $op_values = $params['opvalue'];
		 $now = date("Y-m-d H:i:s");
		 //修改的要保存  之前没勾选现勾选需要增添 之前勾选但现没勾选的要删除
		 //先全删除(pid,poid) 再逐一添加
		 //'delete from product_option_value where product_id=1 and po_id=4 and poc_id not in(1,2,3)';
		 $povServiceObj->deleteByCondition($p_id,$po_id,null);
		 foreach ($poc_ids as $key=>$poc_id){
			 foreach($op_values[$key] as $oval){
				$povServiceObj->add(array('product_id'=>$p_id,'po_id'=>$po_id,'poc_id'=>$poc_id,'po_value'=>$oval,'pov_add_time'=>$now));	 
			 }
		 }
		 $return = array('ask'=>true,'result'=>'Success');
		 die(json_encode($return));
	}

}